﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ESF.General.Entity;
using System.Data;
using ESF.General;
using ESF.Data;

#endregion


namespace ESF.Services.GeneralDirectory.DataAccess
{
	public static class AdoEnterprise
	{
		#region Read

		public static BaseEnterprise GetEnterprise(string pIdentificationTypeCode = null, string pIdentificationNumber = null)
		{
			List<BaseEnterprise> result;


			result = GetEnterprises (pIdentificationTypeCode, pIdentificationNumber);

			if (DataHelper.IsEmpty (result))
			{
				return null;
			}

			return result.First ();
		}


		public static List<BaseEnterprise> GetEnterprises(string pIdentificationTypeCode = null, string pIdentificationNumber = null, string pSearchTerms = null)
		{
			IDbCommand cmd;

			DbReader R = new DbReader ("DB");


			cmd = R.CreateSPCommand ("P_GetEnterprises");

			R.AddParameter (cmd, "pIdentificationTypeCode", pIdentificationTypeCode);
			R.AddParameter (cmd, "pIdentificationTypeNumber", pIdentificationNumber);
			R.AddParameter (cmd, "pSearchTerms", pSearchTerms);

			return R.GetList<BaseEnterprise>(cmd, ConvertTo);
		}


		public static BaseEnterprise ConvertTo(IDataReader pReader, string pPrefix = "", string pListLoading = "")
		{
			return new BaseEnterprise ()
			{
				Name = DataHelper.GetString (pReader, "Ent_Name"),				
				IdentificationNumber = DataHelper.GetString (pReader, "Ent_IdentificationNumber"),
				IdentificationType = new BasicReference () { Code = DataHelper.GetString (pReader, "Ent_IdentificationType_Code") },
				LegalRepresentative = DataHelper.GetString (pReader, "Ent_LegalRepresentative"),
				IdentificationVerificationDigit = DataHelper.GetString (pReader, "Ent_IdentificationVerificationDigit"),
				Email = DataHelper.GetString (pReader, "Ent_Email")
			};
		}

		#endregion
	}
}